package com.zha.model;

import com.zha.constant.BusinessEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author: zha
 * @CreateTime: 2024年10月27日 11:47
 * 项目统一结果对象
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("项目统一结果对象")
public class Result<T> implements Serializable {

    @ApiModelProperty("响应码")
     private Integer code = 200;

    @ApiModelProperty("响应信息")
    private String message = "ok";

    @ApiModelProperty("响应数据")
    private T data;

    /**
     * 操作成功并响应对象
     * @param data
     * @return
     * @param <T>
     */
    public static<T> Result<T> success(T data) {
        Result result = new Result();
        result.setData(data);
        return result;
    }

    /**
     * 操作成功
     * @return
     * @param <T>
     */
    public static<T> Result<T> success() {
        Result result = new Result();
        result.setData(null);
        return result;
    }

    /**
     * 操作失败
     * @param code
     * @param message
     * @return
     * @param <T>
     */
    public static<T> Result<T> fail(Integer code, String message) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    /**
     * 操作失败
     * @param businessEnum
     * @return
     * @param <T>
     */
    public static<T> Result<T> fail(BusinessEnum businessEnum){
        Result result = new Result();
        result.setCode(businessEnum.getCode());
        result.setMessage(businessEnum.getDesc());
        return result;
    }
}
